#! /usr/bin/perl -w

# Nagios plugin to execute commands on remote hosts by GSISSH
# Copyright (c) 2012 Daniel Waldmann, based on Emir Imamagic's nagios probes
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

use strict;
use Nagios::Plugin;
use Sys::Hostname;
use GridMon::sgutils qw($VONAME &checkProxy &processCommand);
use Sys::Syslog;
use TOM::Nagios qw(nagios_debug);

local $SIG{__DIE__}  = \&TOM::Nagios::handle_die;
local $SIG{__WARN__} = \&TOM::Nagios::handle_warn;
openlog("check_by_gsissh", "ndelay,pid", "user");
nagios_debug("started");

# Standard variables used in Nagios::Plugin constructor
use constant PROGNAME => "check_by_gsissh";
use constant VERSION => '0.1';
use constant DESCRIPTION => 'Probe for executing remote commands via GSISSH';
use constant EXTRA_DESC => "";
use constant LICENSE => 'This nagios plugin is free software, and comes with ABSOLUTELY NO WARRANTY.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
   http://www.apache.org/licenses/LICENSE-2.0';
use constant SHORTNAME => 'GSISSH_check';
use constant USAGE => "usage: $0 [ -v ] \n";
use constant DEFAULT_PORT => 2222;
use constant DEFAULT_TMP_DIR => '/tmp';


# Local variables
my ($state,$answer,$res);
my $cmd;

my $gsissh = "/usr/bin/gsissh";
#my $defaultcmd = "/bin/date";
my $defaultcmd = "/bin/pwd";
# Create Nagios::Plugin instance
my $plugin = Nagios::Plugin->new (usage => USAGE,
                                  shortname => SHORTNAME,
                                  version => VERSION,
                                  blurb => DESCRIPTION,
                                  extra => EXTRA_DESC,
                                  license => LICENSE,
                                  plugin  => PROGNAME);

# Define additional arguments
$plugin->add_arg(
    spec => 'hostname|H=s',
    help => "H|hostname\n	Name or IP address of host to check.",
    required => 0,
    default => 'localhost'
);
$plugin->add_arg(
    spec => 'username|u=s',
    help => "u|username\n	User name on remote host.\n (default: current user)",
    required => 0
);
$plugin->add_arg(
    spec => 'port|p=i',
    help => "p|port\n	Port of the service.\n	(default: ".DEFAULT_PORT.")",
    required => 0,
    default => DEFAULT_PORT
);
$plugin->add_arg(
    spec => 'proxy|x=s',
    help => "proxy|x\n	Location of user's proxy file.\n	(default: /tmp/x509up_u$<)",
    required => 0,
    default => "/tmp/x509up_u$<"
);
$plugin->add_arg(
    spec => 'command|c=s',
    help => "c|command\n	Command to execute on remote site.",
    required => 0
);


$plugin->getopts;

my $verbose = $plugin->opts->verbose;

# Just in case of problems, let's not hang Nagios
local $SIG{ALRM} = sub {
    local $SIG{TERM} = 'IGNORE';
    kill TERM => -$$;
    $plugin->nagios_die("Plugin got timeout.");
};

local $SIG{TERM} = sub {
    local $SIG{TERM} = 'IGNORE';
    kill TERM => -$$;
    $plugin->nagios_die("Plugin received TERM signal.");
};


alarm($plugin->opts->timeout);

($state,$answer,$res) = checkProxy($plugin->opts->proxy);
if ( $state != OK) {
    $plugin->nagios_exit($state, $answer);
}

my $username = $plugin->opts->username;

$cmd = $plugin->opts->command;
unless ($cmd) {
	$cmd = $defaultcmd;
}
my $host = $plugin->opts->hostname;
my $port = $plugin->opts->port;
#my $cmdstr = "$gsissh $username\@$host -p $port $cmd";
my $cmdstr;
unless ($username) {
	$cmdstr = "$gsissh $username\@$host -p $port $cmd";
}
$cmdstr = "$gsissh $host -p $port $cmd";


print "COMMAND: $cmdstr \n" if ($verbose);
($state, $res) = processCommand ("$cmdstr 2>&1");

if ($state == CRITICAL) {
	$answer = "Executing command ".$cmd." failed: $res";
} 
elsif ($state == UNKNOWN) {
	$answer = $res;
}
else {
#	print "INFO: Executed command ".$cmd." successfully.\n" if ($verbose);
	$answer = "Command result: ".$res;
}
$plugin->nagios_exit($state,$answer);


